from fastapi import FastAPI
from controller import api_router
from exception_handler import register_exception_handlers
from dependencies import get_mysql_db, get_es
from fastapi.staticfiles import StaticFiles


main_app = FastAPI(
    title='东风态势感知后台系统',
    description='',
    version='0.0.1'
)


# 添加各控制器
main_app.include_router(api_router)
# 注册异常处理器
register_exception_handlers(main_app)


main_app.mount('/static', StaticFiles(directory='static'), name='static')


@main_app.on_event("startup")
async def startup():
    mysql_db = get_mysql_db()
    await mysql_db.connect()


@main_app.on_event("shutdown")
async def shutdown():
    # 关闭 MySQL 的连接
    mysql_db = get_mysql_db()
    await mysql_db.disconnect()
    # 关闭 ES 的连接
    es_client = get_es()
    await es_client.close()
